import { PlaneEvent } from "../Constant/Constant";
import PoolManager from "./PoolManager";

const { ccclass, property } = cc._decorator;

/**
 * 管理玩家飞机初始化、创建、销毁
 */
@ccclass
export default class PlayerManager {

  private playerPlanePrefab: cc.Prefab = null
  private parent: cc.Node = null
  private touchBoomCallback: Function = null
  private playerDiedCallback: Function = null
  /**
   * 飞机初始化
   * @param playerPlanePrefab 飞机预制体
   * @param parent 设置飞机的父节点，默认为当前场景
   */
  public init(playerPlanePrefab: cc.Prefab, parent: cc.Node = cc.director.getScene()): void {
    this.playerPlanePrefab = playerPlanePrefab
    this.parent = parent
    // this.createPlane()
  }

  /**
   * 创建飞机节点，并挂载到指定节点上
   * @param playerPlanePrefab 飞机预制体
   * @param parent 设置飞机的父节点
   */
  public createPlane(): void {
    const playerNode = PoolManager.instance.getNode(this.playerPlanePrefab)
    playerNode.parent = this.parent
    playerNode.setPosition(250, 190)
  }

  /**
   * 设置玩家死亡后的回调函数
   */
  public setPlayerDiedCallback(callback: Function): void {
    if (this.playerDiedCallback != null) {
      this.parent.off(PlaneEvent.DIED, this.playerDiedCallback)
    }

    this.playerDiedCallback = callback
    this.parent.on(PlaneEvent.DIED, this.playerDiedCallback)
  }

  /**
   * 设置玩家碰到Boom道具后的回调函数
   */
  public setTouchBoomCallback(callback: Function): void {
    if (this.touchBoomCallback != null) {
      this.parent.off(PlaneEvent.PROP_BOOM, this.touchBoomCallback)
    }

    this.touchBoomCallback = callback
    this.parent.on(PlaneEvent.PROP_BOOM, this.touchBoomCallback)
  }
}
